﻿using CBS.APP;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// StudentPaymentInfo.xaml 的交互逻辑
    /// </summary>
    public partial class StudentPaymentInfo : Window
    {
        public StudentPaymentViewModel studentPaymentViewModel = new StudentPaymentViewModel();
        public StudentPaymentInfo()
        {
            InitializeComponent();
        }

        public StudentPaymentInfo(int paymentId)
        {
            InitializeComponent();

            studentPaymentViewModel = StudentPaymentApp.GetStudentPaymentInfo(paymentId);

            this.DataContext = studentPaymentViewModel;

            if (!string.IsNullOrEmpty(studentPaymentViewModel.PaymentPic))
            {
                img_Pic.Source = Base64ToImage(studentPaymentViewModel.PaymentPic);
            }
        }

        public static BitmapImage Base64ToImage(string base64String)
        {
            byte[] bytes = Convert.FromBase64String(base64String);
            using (var stream = new MemoryStream(bytes))
            {
                BitmapImage image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = stream;
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.EndInit();
                image.Freeze(); // 使 BitmapImage 可在不同线程中使用
                return image;
            }
        }
    }
}
